package cn.edu.zut.cs.controller;

import cn.edu.zut.cs.pojo.DataVO;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.WeighingVO;
import cn.edu.zut.cs.service.WeighingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
public class VoController {
    @Resource
    private WeighingService weighingService;
    //查询某年的12个月份称重数据
    @RequestMapping("/barVOListByYear/{time}")
    public WeighingVO barVOListByYear(HttpSession session, @PathVariable(value = "time",required = false)Integer year){
        User loginUser = (User) session.getAttribute("loginUser");
        return weighingService.barVOListByYear(loginUser,year);
    }

    //查询某年某月的31天称重数据
    @RequestMapping("/barVOListByMonth/{time}")
    public WeighingVO barVOListByMonth(HttpSession session,@PathVariable(value = "time",required = false)String time) {
        User loginUser = (User) session.getAttribute("loginUser");
        Date date=null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c=Calendar.getInstance();
        c.setTime(date);
        return weighingService.barVOListByMonth(loginUser,c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1);
    }

    @RequestMapping("/barVOListByDay")
    public List<DataVO> barVOListByDay(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        Date time=new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
        c.setTime(time);
        return weighingService.barVOListByDay(loginUser,c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DAY_OF_MONTH));
    }

    @RequestMapping("/barVOListByLastMonth")
    public List<DataVO> barVOListByLastMonth(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        Date time=new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
        c.setTime(time);
        return weighingService.barVOListByLastMonth(loginUser,c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1);
    }


}
